package com.zhy.sample_circlemenu;

import com.zhy.sample_circlemenu.provider.ArrayProvider;
import com.zhy.view.PixelUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

/**
 * ChooserAbility
 *
 * @author hw
 * @since 2021/5/29
 */
public class ChooserAbility
        extends AbilitySlice implements ListContainer.ItemClickedListener {
    private static final int ANIMATOR_DURATION = 200;
    private static final int SHOW_MENU = 100;
    private static final int HIDE_MENU = 200;
    private final List<String> mLabels = new ArrayList<>();
    private AnimatorProperty menuLayoutHideAnimator;
    private AnimatorProperty menuLayoutShowAnimator;
    private DirectionalLayout menuLayout;
    private Component clickToCloseMenuLayout;
    private Component openButton;

    @Override
    protected void onStart(Intent intent) {
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_title_background));
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_chooser);
        getWindow().setStatusBarVisibility(Component.VISIBLE);
        PixelUtil.initContext(this);
        mLabels.add(getString(ResourceTable.String_ccb_cir_menu));
        mLabels.add(getString(ResourceTable.String_cir_menu));
        clickToCloseMenuLayout = findComponentById(ResourceTable.Id_bg_component);
        openButton = findComponentById(ResourceTable.Id_open_button);
        if (findComponentById(ResourceTable.Id_menu_layout) instanceof DirectionalLayout) {
            menuLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_menu_layout);
        }
        Text mId_menu_button = (Text) findComponentById(ResourceTable.Id_menu_button);
        clickToCloseMenuLayout.setClickedListener(component -> setMenuLayoutVisible(false));
        mId_menu_button.setClickedListener(component -> {
            setMenuLayoutVisible(true);
        });
        openButton.setClickedListener(component -> {
            open();
            setMenuLayoutVisible(false);
        });
        if (findComponentById(ResourceTable.Id_list) instanceof ListContainer) {
            final ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list);
            listContainer.setItemProvider(new TypesAdapter());
            listContainer.setItemClickedListener(this);
        }
        initAnimator();
    }

    private void initAnimator() {
        // 设置menu消失的动画
        menuLayoutHideAnimator = menuLayout.createAnimatorProperty();
        menuLayoutHideAnimator.alpha(0).setDuration(ANIMATOR_DURATION);
        // 设置menu显示的动画
        double offsetY = PixelUtil.vp2px(58);
        menuLayoutShowAnimator = menuLayout.createAnimatorProperty();
        menuLayoutShowAnimator
                .moveFromX((float) ((double) PixelUtil.screenWidth() - (menuLayout.getWidth()) - PixelUtil.vp2px(10))).moveToX((float) (((double) PixelUtil.screenWidth()) - (menuLayout.getWidth()) - PixelUtil.vp2px(10)))
                .moveFromY((float) (offsetY - PixelUtil.vp2px(15))).moveToY((float) offsetY)
                .alphaFrom(0).alpha(1)
                .setDuration(ANIMATOR_DURATION);
    }

    /**
     * 设置菜单栏显示/隐藏
     *
     * @param visible 菜单栏是否可见
     */
    private void setMenuLayoutVisible(boolean visible) {
        if (visible) {
            menuLayout.invalidate();
            menuLayoutShowAnimator.start();
            menuLayout.setVisibility(Component.VISIBLE);
            clickToCloseMenuLayout.setVisibility(Component.VISIBLE);
        } else {
            menuLayoutHideAnimator.start();
            menuLayout.setVisibility(Component.HIDE);
            clickToCloseMenuLayout.setVisibility(Component.HIDE);
        }
    }

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int position, long lo) {
        if (mLabels.get(position).equals(getString(ResourceTable.String_ccb_cir_menu))) {
            present(new CCBAbility(), new Intent());
        } else if (mLabels.get(position).equals(getString(ResourceTable.String_cir_menu))) {
            present(new CircleAbility(), new Intent());
        }
    }

    private class TypesAdapter extends ArrayProvider<String> {
        public TypesAdapter() {
            super(ChooserAbility.this, ResourceTable.Layout_item, 0, mLabels);
        }
    }

    private void open() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction("android.intent.action.VIEW")
                .withUri(Uri.parse(getString(ResourceTable.String_url)))
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

}
